package com.tssm.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.tssm.common.dao.impl.BaseDAOImpl;
import com.tssm.dao.SarcInvestigationCargoDAO;
import com.tssm.pojo.SarcInvestigationCargo;
import com.tssm.pojo.SarcInvestigationCargo;
@Repository("sarcinvestigationcargoDAO")
public class SarcInvestigationCargoDAOImpl extends BaseDAOImpl<SarcInvestigationCargo> implements SarcInvestigationCargoDAO {
	
	public List<SarcInvestigationCargo> getInvestigationCargoList(final Long id, final int startIndex, final int pageSize) {
		return (List<SarcInvestigationCargo>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws SQLException, HibernateException {
						String hql = "from SarcInvestigationCargo sinvcargo where sinvcargo.investigationid="+id+" and sinvcargo.dr=0 order by sinvcargo.invcargoid desc";
						Query query = session.createQuery(hql);
						query.setFirstResult(startIndex);
						query.setMaxResults(pageSize);
						return query.list();
					}
				});
	}
	
	public int getInvestigationCargoListCount(final Long id) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws SQLException, HibernateException {
						String hql = "select count(sinvcargo.invcargoid) from SarcInvestigationCargo sinvcargo where sinvcargo.investigationid="+id+" and sinvcargo.dr=0";						
						Query query = session.createQuery(hql);
						Long l = (Long) query.uniqueResult();
						return l.intValue();
					}
				});
	}
	
	public SarcInvestigationCargo getInvestigationCargoById(final Long id) {
		SarcInvestigationCargo sinvcargo = null;
		String hql = "from SarcInvestigationCargo sinvcargo where sinvcargo.invcargoid=" + id.toString() + " and sinvcargo.dr=0";
		List<SarcInvestigationCargo> sinvcargolist = super.find(hql);
		if(sinvcargolist.size() > 0) sinvcargo = sinvcargolist.get(0);
		return sinvcargo;		
	}

}
